<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.mousemove()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/events/">事件</a> &gt; <a href="/category/events/mouse-events/">鼠标事件</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/mousemove/" target="_blank">.mousemove()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="mousemove1"><h2 class="section-title">
<span class="name">.mousemove( handler(eventObject) )</span><span class="returns">返回: <a href="/Types/#jQuery">jQuery</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>为 JavaScript 的 "mousemove" 事件绑定一个处理函数，或者触发元素上的该事件。</p>
<ul class="signatures">
<li class="signature" id="mousemove-handlereventObject">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.mousemove( handler(eventObject) )</h4>
<ul><li>
<div><strong>handler(eventObject)</strong></div>
<div>类型: <a href="/Types/#Function">Function</a>()</div>
<div>每次事件触发时会执行的函数。</div>
</li></ul>
</li>
<li class="signature" id="mousemove-eventData-handlereventObject">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.4.3/">1.4.3</a></span>.mousemove(  [eventData ], handler(eventObject) )</h4>
<ul>
<li>
<div><strong>eventData</strong></div>
<div>类型: <a href="/Types/#PlainObject">PlainObject</a>
</div>
<div>一个对象，它包含的数据键值对映射将被传递给事件处理程序。</div>
</li>
<li>
<div><strong>handler(eventObject)</strong></div>
<div>类型: <a href="/Types/#Function">Function</a>()</div>
<div>每次事件触发时会执行的函数。</div>
</li>
</ul>
</li>
<li class="signature" id="mousemove">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.mousemove()</h4>
<ul><li><div class="null-signature">这个方法不接受任何参数。</div></li></ul>
</li>
</ul>
<div class="longdesc" id="entry-longdesc">
<p>这个方法的前两个用法是 <code>.bind('mousemove', handler)</code> 的快捷方式，第3个不带参数的用法是 <code>.trigger('mousemove')</code> 的快捷方式。</p>
<p>当鼠标指针在元素内移动时，<code>mousemove</code>事件就会被触发，任何HTML元素都可以接受此事件。</p>
<p>举例来说，请看下面的HTML：</p>
<div class="syntaxhighlighter xml nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">id</span>=<span class="value">"target"</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  Move here</code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">id</span>=<span class="value">"other"</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  Trigger the handler</code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">id</span>=<span class="value">"log"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p class="image"><img alt="" src="images/0042_05_10.png"/></p>
<p>这个事件可以绑定到目标元素:</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(<span class="string">"#target"</span>).mousemove(<span class="keyword">function</span>(event) {</code></div></div><div class="container"><div class="line"><code>  <span class="keyword">var</span> msg = <span class="string">"Handler for .mousemove() called at "</span>;</code></div></div><div class="container"><div class="line"><code>  msg += event.pageX + <span class="string">", "</span> + event.pageY;</code></div></div><div class="container"><div class="line"><code>  $(<span class="string">"#log"</span>).append(<span class="string">"&lt;div&gt;"</span> + msg + <span class="string">"&lt;/div&gt;"</span>);</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>现在当鼠标指针在目标元素中移动时，以下信息将被添加到 &lt;div id="log"&gt;:</p>
<p>
<span class="output">Handler for .mousemove() called at (399, 48)</span>
<br/>
<span class="output">Handler for .mousemove() called at (398, 46)</span>
<br/>
<span class="output">Handler for .mousemove() called at (397, 44)</span>
<br/>
<span class="output">Handler for .mousemove() called at (396, 42)</span>
<br/>
</p>
<p>使用不带参数的<code>.mousemove()</code>，我们可以手动触发这个事件:</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$(<span class="string">"#other"</span>).click(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  $(<span class="string">"#target"</span>).mousemove();</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>在执行完上述代码之后，点击 Trigger 按钮同样会追加如下信息：</p>
<p>
<span class="output">Handler for .mousemove() called at (undefined, undefined)</span>
</p>
<p>当跟踪鼠标移动时，我们通常需要知道实际的鼠标指针的位置。event 对象传递给处理程序包含了一些有关鼠标的坐标信息。比如<code>.clientX</code>, <code>.offsetX</code>, 和 <code>.pageX</code>属性是有效的，但对他们的支持不同浏览器。幸运的是，jQuery的规范了<code>.pageX</code>和<code>.pageY</code>属性，以便他们能够在所有浏览器上使用。这些属性提供了鼠标指针位置相对于页面的左上角的X和Y坐标，例如上面例子输出显示的坐标值。</p>
<p>我们必须记住， <code>mousemove</code>事件是当鼠标指针移动时触发的，即使是一个像素。这意味着多个事件在短时间内被触发。如果处理器做任何重大的处理，或者如果该事件存在多个处理函数，这可能造成浏览器的严重的性能问题。因此，优化<code>mousemove</code>处理程序尽可能，这一点很重要，当不再需要他们时应尽快解除绑定。</p>
<p>一个常见的模式是在<code>mousedown</code>处理器内部绑定<code>mousemove</code>处理器，并在一个相应<code>mouseup</code>处理函数解除绑定。要实现这一系列事件，请记住，与 <code>mouseup</code> 事件相比，<code>mouseup</code> 事件可能会被发送到不同的 HTML 元素上。由于这个原因，<code>mouseup</code> 事件通常应该绑定在更高的 DOM 树中，例如 <code>&lt;body&gt;</code>。</p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">当在黄色的 div 移动鼠标时，显示鼠标指针的坐标。坐标是相对于窗口的，在该例中，窗口指的是 iframe。</span></h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
<div class="line n26">26</div>
<div class="line n27">27</div>
<div class="line n28">28</div>
<div class="line n29">29</div>
<div class="line n30">30</div>
<div class="line n31">31</div>
<div class="line n32">32</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code><span class="tag">div</span> <span class="rules">{ <span class="rule"><span class="attribute">width</span>:<span class="value"><span class="number">220</span>px;</span></span> <span class="rule"><span class="attribute">height</span>:<span class="value"><span class="number">170</span>px;</span></span> <span class="rule"><span class="attribute">margin</span>:<span class="value"> <span class="number">10</span>px <span class="number">50</span>px <span class="number">10</span>px <span class="number">10</span>px;</span></span></span></code></div></div><div class="container"><div class="line"><code>      <span class="rule"><span class="attribute">background</span>:<span class="value">yellow;</span></span> <span class="rule"><span class="attribute">border</span>:<span class="value"><span class="number">2</span>px groove;</span></span> <span class="rule"><span class="attribute">float</span>:<span class="value">right;</span></span> <span class="rule">}</span></code></div></div><div class="container"><div class="line"><code><span class="tag">p</span> <span class="rules">{ <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">0</span>;</span></span> <span class="rule"><span class="attribute">margin-left</span>:<span class="value"><span class="number">10</span>px;</span></span> <span class="rule"><span class="attribute">color</span>:<span class="value">red;</span></span> <span class="rule"><span class="attribute">width</span>:<span class="value"><span class="number">220</span>px;</span></span></span></code></div></div><div class="container"><div class="line"><code>    <span class="rule"><span class="attribute">height</span>:<span class="value"><span class="number">120</span>px;</span></span> <span class="rule"><span class="attribute">padding-top</span>:<span class="value"><span class="number">70</span>px;</span></span></code></div></div><div class="container"><div class="line"><code>    <span class="rule"><span class="attribute">float</span>:<span class="value">left;</span></span> <span class="rule"><span class="attribute">font-size</span>:<span class="value"><span class="number">14</span>px;</span></span> <span class="rule">}</span></code></div></div><div class="container"><div class="line"><code><span class="tag">span</span> <span class="rules">{ <span class="rule"><span class="attribute">display</span>:<span class="value">block;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">p</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">span</span>&gt;</span>Move the mouse over the div.<span class="tag">&lt;/<span class="title">span</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">span</span>&gt;</span>&amp;nbsp;<span class="tag">&lt;/<span class="title">span</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">p</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span></code></div></div><div class="container"><div class="line"><code>$(<span class="string">"div"</span>).mousemove(<span class="keyword">function</span>(e){</code></div></div><div class="container"><div class="line"><code>  <span class="keyword">var</span> pageCoords = <span class="string">"( "</span> + e.pageX + <span class="string">", "</span> + e.pageY + <span class="string">" )"</span>;</code></div></div><div class="container"><div class="line"><code>  <span class="keyword">var</span> clientCoords = <span class="string">"( "</span> + e.clientX + <span class="string">", "</span> + e.clientY + <span class="string">" )"</span>;</code></div></div><div class="container"><div class="line"><code>  $(<span class="string">"span:first"</span>).text(<span class="string">"( e.pageX, e.pageY ) : "</span> + pageCoords);</code></div></div><div class="container"><div class="line"><code>  $(<span class="string">"span:last"</span>).text(<span class="string">"( e.clientX, e.clientY ) : "</span> + clientCoords);</code></div></div><div class="container"><div class="line"><code>});</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo" data-height="300"></div>
</div></section>
</div></article> </div>
</article>